package com.yanqu.road.server.cmd.activity.pawnshop;

import com.yanqu.road.entity.activity.pawnshop.PawnshopItem;
import com.yanqu.road.entity.activity.pawnshop.UserPawnshopEvent;
import com.yanqu.road.entity.activity.pawnshop.UserPawnshopItem;
import com.yanqu.road.entity.enums.activity.pawnshop.ePawnshopWay;
import com.yanqu.road.entity.enums.activity.pawnshop.ePawnshopEventType;
import com.yanqu.road.entity.enums.eChatContentType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMessageType;
import com.yanqu.road.pb.activity.PawnshopProto.PawnshopEventGemstoneConfirmReqMsg;
import com.yanqu.road.pb.activity.PawnshopProto.PawnshopEventGemstoneConfirmRespMsg;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.pawnshop.PawnshopModule;
import com.yanqu.road.server.manger.activity.pawnshop.PawnshopMgr;
import com.yanqu.road.server.manger.player.WorldMessageMgr;
import com.yanqu.road.server.pb.WorldMessagePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_PAWNSHOP_EVENT_GEMSTONE_CONFIRM, desc = "请求赌石事件确认")
public class PawnshopEventGemstoneConfirmCmd extends AbstractCommand {

    private boolean isConfirm = false;// 是否确认
    private long itemIdSel = 0;// 选择的物件ID
    private long itemIdReward = 0;// 获得的物件ID
    private long coinCost = 0;// 花费的当铺币

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        PawnshopEventGemstoneConfirmReqMsg reqMsg = PawnshopEventGemstoneConfirmReqMsg.parseFrom(packet.getBytes());
        isConfirm = reqMsg.getIsConfirm();
        itemIdSel = reqMsg.getItemId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (!PawnshopMgr.isActivityInTime()) {
            // 非活动时间内
            this.ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        PawnshopModule psModule = player.getModule(PawnshopModule.class);
        if (!psModule.getUserPawnshop().isReception()) {
            // 顾客未接待过
            this.ret = GameErrorCode.E_PAWNSHOP_NOT_RECEPTION;
            return;
        }
        UserPawnshopEvent userPawnshopEvent = psModule.getUserPawnshopEvent();
        if (userPawnshopEvent.getType() != ePawnshopEventType.Gemstone.getValue()) {
            ret = GameErrorCode.E_PAWNSHOP_EVENT_ERROR;
            return;
        }
        if (isConfirm) {
            // 确认成功
            PawnshopItem itemJSON = psModule.getPawnshopItemJSON(itemIdSel);
            if (itemJSON == null) {
                // 不存在的物件
                this.ret = GameErrorCode.E_PAWNSHOP_NOT_ITEM;
                return;
            }
            itemIdReward = itemJSON.getItemId();
            coinCost = itemJSON.getCoinBuy();
             ret = psModule.coinCost(coinCost, eLogMoneyType.PawnshopGemstone.getValue());
            if (ret != 0) {
                return;
            }
            UserPawnshopItem item = new UserPawnshopItem();
            item.setItemId(System.currentTimeMillis());
            item.setPawnshopItem(itemJSON);
            item.setDescNo(PawnshopMgr.randomItemDesc(item.getGoodsId()));
            psModule.addItem(item, eLogMoneyType.PawnshopGemstone.getValue());
            if (itemJSON.isSpecial()) {
                int goodsId = itemJSON.getGoodsId();
                long valuationPrice = PawnshopMgr.calcItemValuationPrice(itemJSON, psModule.getUserPawnshop().getPoliticsAddition(), 0);
                WorldMessageProto.WorldMessageTempMsg.Builder msg = WorldMessagePb.parseWorldMessageTempMsg(
                        player.getUserInfo(), eMessageType.YongAnDangDuShi.getValue(), eChatContentType.Common.getContentType(),goodsId + "|" + valuationPrice,
                        System.currentTimeMillis(), PawnshopMgr.config.getActivityId(), null);
                WorldMessageMgr.sendCrossActivityChat(msg);
            }
        }
        psModule.afterReception();
    }

    @Override
    public void sendData(GamePlayer player) {
        PawnshopEventGemstoneConfirmRespMsg.Builder builder = PawnshopEventGemstoneConfirmRespMsg.newBuilder();
        builder.setRet(ret);
        builder.setIsConfirm(isConfirm);
        if (ret == 0) {
            builder.setItemId(itemIdReward);
            builder.setCoinCost(coinCost);
        }
        player.sendPacket(ClientProtocol.U_PAWNSHOP_EVENT_GEMSTONE_CONFIRM, builder);
    }
}
